#coding: utf-8

import cocos2d
import cocos2d.extension

class A(object):
    def cb_test(self, dt):
        print dt

a = A()

timer = cocos2d.Timer()
timer.Interval = 0.3
timer.Selector = a.cb_test

def fill_ani(ani, format, nStartIndex = 0):
    i = nStartIndex
    while True:
        szRes = format % i
        print szRes
        spf = cocos2d.sharedSpriteFrameCache.spriteFrameByName(szRes)
        print spf
        if spf is None:
            break
        ani.addSpriteFrame(spf)
        i += 1

def get_ani(format):
    ani = cocos2d.sharedAnimationCache.animationByName(format)
    if not ani:
        ani = cocos2d.Animation()
        cocos2d.sharedAnimationCache.addAnimation(ani, format)
        fill_ani(ani, format, 1)
    return ani


def on_click(obj):
    print 'click', obj
    cocos2d.sharedDirector.end()

class mydele(cocos2d.extension.ScrollViewDelegate):
    def __init__(self):
        pass

    def scrollViewDidScroll(self, view):
        print 'scrollViewDidScroll', view

    def scrollViewDidZoom(self, view):
        print 'scrollViewDidScroll', scrollViewDidZoom

class myscene(cocos2d.Scene):
    def __init__(self):
        super(myscene,self).__init__()
        layer = cocos2d.LayerColor(cocos2d.Color4B(250,250,250,150))
        #layer.initWithColor()
        layer.Tag = 0x101
        #self.addChild(layer, 1)

        ttf = cocos2d.LabelTTF('test','Verdana', 24)
        #ttf.Position = cocos2d.Point(480 / 2,320 / 2)
        #ttf.Tag = 0x100
        #self.addChild(ttf, 2)

        rotate = cocos2d.RotateBy(0.5, 360)
        rotate2 = cocos2d.RotateBy(0.5, 360)
        act = cocos2d.MoveTo(3, cocos2d.Point(100,100))

        cf = cocos2d.CallFunc(ttf.removeFromParent)
        act = cocos2d.Sequence((rotate, act, cf))
        #ttf.runAction(cocos2d.RepeatForever(rotate))
        animation = get_ani('monster/1001/%d.png')
        animation.DelayPerUnit = 0.1
        #ttf.runAction(cocos2d.RepeatForever(cocos2d.Animate(animation)))

        #ttf.runAction(act)

        #self.scheduleUpdate()
        spr = cocos2d.Sprite("HelloWorld.png")
        #self.addChild(spr)
        pt = cocos2d.Point(480 / 2,320 / 2)
        spr.Position = pt
        print spr.Texture, cocos2d.Texture2D.defaultAlphaPixelFormat()

        item1 = cocos2d.MenuItemImage('ui/Menu_Start.png', 'ui/Menu_Start_hover.png', '', on_click, True)
        item2 = cocos2d.MenuItemImage('ui/Menu_Exit.png', 'ui/Menu_Exit_hover.png', '', on_click, True)
        menu = cocos2d.Menu((item1,item2))
        menu.alignItemsVerticallyWithPadding(0)
        #self.addChild(menu)
        si = cocos2d.sharedOpenGLView.getVisibleSize()
        menu.Position = cocos2d.Point(si.width / 2,  si.height / 2)

        layer = cocos2d.Layer()

        layer.addChild(menu)

        md = mydele()
        SV = cocos2d.extension.ScrollView(cocos2d.Size(300,400), layer)
        SV.Delegate = md
        self.addChild(SV)

        pass

    def __del__(self):
        print 'del'

    def update(self, dt):
        #timer.update(dt)
        #print 'myscene update'
        #ttf = self.getChildByTag(0x100)
        #ttf = ttf.typeto(cocos2d.LabelTTF)
        #print ttf, self.getChildByTag(0x1011)
        #print cocos2d.sharedOpenGLView.getFrameSize()
        pass

def game_main():
    cocos2d.sharedSpriteFrameCache.addSpriteFramesWithFile("resource.plist")
    scene =  myscene()
    #cocos2d.sharedOpenGLView.setDesignResolutionSize(480, 320, cocos2d.kResolutionExactFit)
    #cocos2d.sharedOpenGLView.setFrameSize(960, 640)
    cocos2d.sharedDirector.runWithScene(scene)
    pass


print '============= game start ============='
print '>>> cocos2d-x version: %d'%(cocos2d.COCOS2D_VERSION)
print '>>> target platform: %d'%(cocos2d.CC_TARGET_PLATFORM)

game_main()